﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WPFOpenVideo.Model;
using WPFOpenVideo.Bll;
using Application=System.Windows.Forms.Application;
using System.Xml.Linq;
using System.Windows.Threading;

namespace WPFOpenVideo
{
    /// <summary>
    /// AddPointForButton.xaml 的交互逻辑
    /// </summary>
    public partial class AddPointForButton : Window
    {

        List<AddPointForButtonModel> list_AddPointForButtonModel ;
        private ObservableCollection<AddPointForButtonModel> listView_obc_list;
        private List<AddPointForButtonModel> listView_list;
        private AddPointForButtonBll new_AddPointFORButtonBll;

        private string path;
        public AddPointForButton()
        {
            InitializeComponent();
            list_AddPointForButtonModel = new List<AddPointForButtonModel>();
            new_AddPointFORButtonBll = new AddPointForButtonBll();
            listView_obc_list = new ObservableCollection<AddPointForButtonModel>();
            listView_list = new List<AddPointForButtonModel>();
            path = String.Format($@"{Application.StartupPath}\Config\AddPointForButton.xml");
        }

        /// <summary>
        /// 委托函数对象，处理ListView流式加载
        /// </summary>
        /// <param name="list_xelement"></param>
        private int n = 0;//测试
        public void CallbackToUI(List<XElement> list_xelement)
        {
            foreach (var item in list_xelement)
            {
                listView_list.Add(new AddPointForButtonModel()
                {
                    Id = Convert.ToInt32(item.Attribute("Id").Value),
                    PlcField = item.Element("PlcField").Value,
                    VideoSource = item.Element("VideoSource").Value,
                    VideoName = item.Element("VideoName").Value
                });
            }

            ListView1.Dispatcher.InvokeAsync(() =>
            {
                foreach (var item in listView_list)
                {
                    Thread.Sleep(2000);
                    listView_obc_list.Add(item);
                    Console.WriteLine($"{n}异步回到UI主线程填充ListView成功");
                    n++;//测试
                }
            },DispatcherPriority.Background);
        }


        private void ListView1_Loaded(object sender, RoutedEventArgs e)
        {
            new_AddPointFORButtonBll.ReadXmlAsynBll(path, CallbackToUI);
            ListView1.ItemsSource = listView_obc_list;
            
        }

        private void SelectFile_Click(object sender, RoutedEventArgs e)
        {

        }

        private void DeleteTarget_Click(object sender, RoutedEventArgs e)
        {

        }

        private void ChargeTarget_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
